package com.xyazm.order.manager;

import com.xyazm.order.domain.entity.AfterSaleItemDO;
import com.xyazm.order.domain.entity.OrderItemDO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 售后金额计算
 * @Author: xuyou
 * @Date: 2022/7/6 15:51
 */
@Component
public class AfterSaleAmountManager {
    /**
     * 计算订单条目缺品实际退款金额
     * 计算公式：（缺品数量/下单数量） * 原付款金额
     * @param orderItemDO
     * @param lackNum
     * @return
     */
    public Integer calculateOrderItemLackRealRefundAmount(OrderItemDO orderItemDO, Integer lackNum) {
        double rate = lackNum / orderItemDO.getSaleQuantity().doubleValue();
        //金额向上取整
        Integer itemRefundAmount = Double.valueOf(Math.ceil(orderItemDO.getPayAmount() * rate)).intValue();
        return itemRefundAmount;
    }

    /**
     * 计算订单总申请退款金额
     * @param lackItems
     * @return
     */
    public Integer calculateOrderLackApplyRefundAmount(List<AfterSaleItemDO> lackItems) {
        Integer applyRefundAmount = 0;
        for (AfterSaleItemDO item : lackItems) {
            applyRefundAmount += item.getApplyRefundAmount();
        }
        return applyRefundAmount;
    }

    /**
     * 计算订单总实际退款金额
     * @param lackItems
     * @return
     */
    public Integer calculateOrderLackRealRefundAmount(List<AfterSaleItemDO> lackItems) {
        Integer realRefundAmount = 0;
        for (AfterSaleItemDO item : lackItems) {
            realRefundAmount += item.getRealRefundAmount();
        }
        return realRefundAmount;
    }
}
